package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author zzw
 * @date 2025/4/29
 * @description 套餐管理控制器
 */
@Api(tags = "套餐管理接口")
@Slf4j
@RequestMapping("admin/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDTO 套餐信息
     * @return Result<String>
     */
    @CacheEvict(cacheNames = "setmeal", key = "#setmealDTO.categoryId")
    @ApiOperation("新增套餐")
    @PostMapping
    public Result<String> save(@ApiParam(value = "套餐信息", required = true) @RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐：{}", setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询套餐
     *
     * @param setmealPageQueryDTO 套餐查询参数
     * @return Result<PageResult < SetmealVO>>
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult<SetmealVO>> page(
            @ApiParam(value = "套餐分页查询参数", required = true) SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询：{}", setmealPageQueryDTO);
        PageResult<SetmealVO> pageResult = setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO 套餐DTO
     * @return Result<String>
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("修改套餐信息")
    @PutMapping
    public Result<String> update(
            @ApiParam(value = "套餐修改信息", required = true) @RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐：{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     *
     * @param id 套餐id
     * @return Result<SetmealVO>
     */
    @ApiOperation("根据ID查询套餐详情")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(
            @ApiParam(value = "套餐ID", required = true, example = "1") @PathVariable Long id) {
        log.info("根据id查询套餐：{}", id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐状态
     *
     * @param status 状态
     * @param id     套餐id
     * @return Result<String>
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("启用/停用套餐")
    @PostMapping("/status/{status}")
    public Result<String> status(@ApiParam(value = "状态(0停用1启用)", required = true) @PathVariable Integer status,
                                 @ApiParam(value = "套餐ID", required = true) Long id) {
        log.info("修改套餐状态：{}", status);
        setmealService.status(status, id);
        return Result.success();
    }

    /**
     * 批量删除套餐
     *
     * @param ids 套餐id数组
     * @return Result<String>
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    public Result<String> deleteBatch(@ApiParam(value = "套餐ID数组", required = true) Long[] ids) {
        log.info("删除套餐：{}", Arrays.asList(ids));
        setmealService.deleteBatch(ids);
        return Result.success();
    }
}
